var express = require('express');
var router = express.Router();
const pro = require("../sql/pro")
const sql = require("../sql/sql")


/**
* @apiGroup 商品信息
* @api {get} /api/goods/get 获取所有商品数据
* @apiVersion 0.0.0
* @apiDescription 请求所有商品数据
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} data 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "code":1,
*    "title":"请求成功",
*    "data":[{},{},...]
* }
* @apiError {number} code 请求状态
* @apiError {string} title 请求说明
* @apiError {array} data 请求数据
* @apiErrorExample {json} Error-Response:
* {
*    "code": 0,
*    "title": "请求失败",
*    "data": null
* }
* @apiSampleRequest http://localhost:3000/api/goods/get
*/
// 获取所有商品数据
router.get('/get', function(req, res, next) {
    sql.get(pro).then(data=>{
        res.send({
            code: 1,
            title:"获取成功",
            data:data
        })
    }).catch(err=>{
        res.send({
            code: 0,
            title: err,
            data: null
        })
    })
});


/**
* @apiGroup 商品信息
* @api {get} /api/goods/detail 获取商品详情数据
* @apiVersion 0.0.0
* @apiDescription 根据商品id获取商品详情数据
* @apiParam {String} id 要获取详情的商品的id
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} data 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "code":1,
*    "title":"请求成功",
*    "data":{}
* }
* @apiSampleRequest http://localhost:3000/api/goods/detail
*/
// 获取指定商品详情
router.get('/detail', function(req, res, next) {
    const id = req.query.id;
    sql.get(pro, {proId: id}).then(data=>{
        res.send({
            code: 1,
            title:"获取成功",
            data:data[0]
        })
    }).catch(err=>{
        res.send({
            code: 0,
            title: err,
            data: null
        })
    })
});


// 根据搜索关键字，获取商品
router.get('/search', function(req, res, next) {
    const wd = req.query.wd;
    const reg = new RegExp(wd);
    sql.get(pro, {$or:[{name:reg},{message:reg}]}).then(data=>{
        res.send({
            code: 1,
            title:"获取成功",
            data:data
        })
    }).catch(err=>{
        res.send({
            code: 0,
            title: err,
            data: null
        })
    })
});

// 获取分页数据
router.get('/page', function(req, res, next) {
    const index = req.query.index-0;
    const num = req.query.num-0;
    sql.getPage(pro, {}, index, num).then(data=>{
        res.send({
            code: 1,
            title:"获取成功",
            data:data
        })
    }).catch(err=>{
        res.send({
            code: 0,
            title: err,
            data: null
        })
    })
});

// 获取排序数据
router.get('/sort', function(req, res, next) {
    const s = req.query;

    sql.getSort(pro, {}, s).then(data=>{
        res.send({
            code: 1,
            title:"获取成功",
            data:data
        })
    }).catch(err=>{
        res.send({
            code: 0,
            title: err,
            data: null
        })
    })
});


// 区间查找数据
router.get('/range', function(req, res, next) {
    const wd = req.query.wd;
    const min = req.query.min-0;
    const max = req.query.max-0;

    sql.get(pro, {[wd]: {$lte: max, $gte: min}}).then(data=>{
        res.send({
            code: 1,
            title:"获取成功",
            data:data
        })
    }).catch(err=>{
        res.send({
            code: 0,
            title: err,
            data: null
        })
    })
});



module.exports = router;
